package com.gxuwz.medical.web.servlet;

import java.io.IOException;
import java.io.OutputStream;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.util.CellRangeAddress;

import com.gxuwz.medical.dao.ChronicSyndromeDao;
import com.gxuwz.medical.dao.ChronicdisApplyDao;
import com.gxuwz.medical.domain.chronicSyndrome.ChronicSyndrome;
import com.gxuwz.medical.domain.chronicdisApply.ChronicdisApply;
import com.gxuwz.medical.domain.house.House;
import com.gxuwz.medical.domain.member.Member;
import com.gxuwz.medical.exception.DbException;

public class ChronicSyndromeServlet extends BaseServlet {

	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String m = request.getParameter("m");
		if ("list".equals(m)) {
			process(request, response, "/page/chronicSyndrome/chronicSyndrome_list.jsp");
		}
		
/*		else if("select".equals(m)){
			select(request,response);
		}
*/		
		else if("input".equals(m)){
			process(request, response, "/page/chronicSyndrome/chronicSyndrome_add.jsp");
		}else if("add".equals(m)){
			try {
				add(request,response);
				process(request, response, "/page/chronicSyndrome/chronicSyndrome_list.jsp");
			}catch (Exception e) {
				e.printStackTrace();
			}
		}else if("del".equals(m)){
			try {
				del(request,response);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
/*	
	protected void select(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		try {
			// 定义一个sb来接收模糊查询的sql
			StringBuffer sb = new StringBuffer(1024);
			// form表单提交过来的用户输入的title模糊查询关键字
			String nzhcode = request.getParameter("nzhcode");
			// System.out.println("housemaster:" + housemaster);
			
			// 如果housemaster有输入的时候，才拼接sql进行查询
			 if (nzhcode != null && !"".equals(nzhcode)) {
				sb.append(" nzhcode like '%" + nzhcode + "%'");
			}
			 String mxbzcode = request.getParameter("mxbzcode");
			 if(mxbzcode!=null&&!"".equals(mxbzcode)){
				 sb.append("mxbzcode like '%"+mxbzcode + "%'");
			 }
			 String idcard = request.getParameter("idcard");
			 if(idcard!=null&&!"".equals(idcard)){
				 sb.append("idcard like '%"+idcard + "%'");
			 }
			 String startTime = request.getParameter("startTime");
			 if(startTime!=null&&!"".equals(startTime)){
				 sb.append("startTime >= '%"+startTime + "%'");
			 }
			 
			 String endTime = request.getParameter("endTime");
			 if(endTime!=null&&!"".equals(endTime)){
				 sb.append("endTime <= '%"+endTime + "%'");
			 }
			List<ChronicSyndrome> newsList = ChronicSyndrome.getNewsList(sb.toString());

			request.setAttribute("newsList", newsList);
			request.getRequestDispatcher("/page/chronicSyndrome/chronicSyndrome_list.jsp")
					.forward(request, response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
*/	
	
	
	protected void add(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
	String idcard = request.getParameter("perscode");
//	System.out.println("idcard:"+idcard);
//	String mxbzcode = request.getParameter("mxbzcode");
	String nhzcode = request.getParameter("nhzcode");
//	System.out.println("nhzcode:"+nhzcode);
	String name = request.getParameter("persname");
//	System.out.println("name:"+name);
	String chronicname = request.getParameter("chronicname");
	String startTime = request.getParameter("startTime");
	String endTime = request.getParameter("endTime");
	String creator = (String) request.getSession().getAttribute("userName");
	
	ChronicSyndrome model = new ChronicSyndrome();
//	model.setMxbzcode(mxbzcode);
	model.setName(name);
	model.setChronicname(chronicname);
	model.setMember(new Member(idcard));
	model.setIdcard(idcard);
	model.setStartTime(startTime);
	model.setEndTime(endTime);
	model.setCreator(creator);
	model.setStatus("1");// 档案状态：1表示正常
	model.addChronicSyndrome();
	}
	
	protected void del(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
	String mxbzcode = request.getParameter("id");
	
	ChronicSyndrome cs = new ChronicSyndrome();
	cs.setMxbzcode(mxbzcode);
	cs.del();
	process(request, response, "/page/chronicSyndrome/chronicSyndrome_list.jsp");
	}
/*
	// 导出Excel
		 private void Excel(HttpServletRequest req, HttpServletResponse response) throws IOException, DbException {
			 
			  String[] tableHeaders={"农合证号","慢性病证号","身份证号","疾病名称","开始时间","结束时间","持证人姓名","状态","创建时间","创建人"};
				
			    // 1:创建一个workbook  
			    HSSFWorkbook workbook = new HSSFWorkbook();
			    HSSFSheet sheet = workbook.createSheet("Sheet1");
			    
			    // 2:设置每一列的宽度
			    sheet.setDefaultColumnWidth(20);  
		        sheet.setColumnWidth(0,5120);
		        sheet.setColumnWidth(1,5120);
		        sheet.setColumnWidth(2,5120);
		        sheet.setColumnWidth(3,5120);
		        sheet.setColumnWidth(4,5120);
		        sheet.setColumnWidth(5,5120);
		        sheet.setColumnWidth(6,5120);
		        sheet.setColumnWidth(7,5120);
		        sheet.setColumnWidth(8,5120);
		        sheet.setColumnWidth(9,5120);

			    // 3:创建头部样式  
		        HSSFCellStyle cellStyle = workbook.createCellStyle();    
		        Font font = workbook.createFont();
			    //font.setColor(HSSFColor.BLACK.index);  
			    cellStyle.setFont(font);
			    cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);  //水平居中
		//	    cellStyle.setVerticalAlignment(cellStyle.getVerticalAlignmentEnum().CENTER); // 垂直居中 
			    
			    // 4:创建文件体样式
		        HSSFCellStyle style1 = workbook.createCellStyle();  
		        style1.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 水平居中  
		 //       style1.setVerticalAlignment(style1.getVerticalAlignmentEnum().CENTER); // 垂直居中  
		        
			    // 5:将第一行的7个单元格给合并
			    sheet.addMergedRegion(new CellRangeAddress(0,0,0,4));  //四个参数分别代表  行开始 行结束 列开始 列结束
			    HSSFRow row = sheet.createRow(0); //创建第一行
			    row.setHeightInPoints(20);//设置行高
			    HSSFCell beginCell = row.createCell(0);
			    beginCell.setCellValue("慢性病证导出");  //赋值
			    beginCell.setCellStyle(cellStyle); //赋样式
			    
		        
		        
		        row = sheet.createRow(1);  //第二行
		        row.setHeightInPoints(20);//设置高度
			    // 创建表头
			    for (int i = 0; i < tableHeaders.length; i++) {
			        HSSFCell cell = row.createCell(i); //找到单元格
			        cell.setCellValue(tableHeaders[i]); //给单元格赋值
			        cell.setCellStyle(cellStyle);  
			    }
			    
			    ChronicSyndromeDao chronicSyndromeDao = new ChronicSyndromeDao();
			    List<ChronicSyndrome>listChronicSyndrome = chronicSyndromeDao.ChronicSyndromeList();
			    for(int i=0; i<listChronicSyndrome.size();i++){
			    	int index = 0;
			    	row = sheet.createRow(i+2);  //行数累加
			    	row.setHeightInPoints(20);
			    	HSSFCell cell = row.createCell(index++); //找到这一行的每一列
			    	cell.setCellValue(listChronicSyndrome.get(i).getn);//赋值
			    	cell.setCellStyle(style1);
			    	HSSFCell cell1 = row.createCell(index++);
			    	cell1.setCellValue(listChronicSyndrome.get(i).getApplyAcount().doubleValue());
			    	cell1.setCellStyle(style1);
			    	HSSFCell cell2 = row.createCell(index++);
			    	cell2.setCellValue(listChronicSyndrome.get(i).getName());
			    	cell2.setCellStyle(style1);
			    	HSSFCell cell3 = row.createCell(index++);
			    	cell3.setCellValue(listChronicSyndrome.get(i).getChronicname());
			    	cell3.setCellStyle(style1);
			    	HSSFCell cell4 = row.createCell(index++);
			    	cell4.setCellValue(listChronicSyndrome.get(i).getCreatetime());
			    	cell4.setCellStyle(style1);
			    	
			    }
			    
			    OutputStream outputStream = response.getOutputStream();  //输出
			    response.reset(); 
			    response.setContentType("application/vnd.ms-excel");    //设置头部信息
			    response.setHeader("Content-disposition", "attachment;filename = ChronicdisApply.xls");

			    workbook.write(outputStream);
			    outputStream.flush();    
			    outputStream.close();
			    

		}
		*/
}
